[% setvar title Line Disciplines %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>Line Disciplines</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: Simon Cozens &lt;<a href='mailto:simon@brecon.co.uk'>simon@brecon.co.uk</a>&gt;
  Date: 25 Sep 2000
  Mailing List: <a href='mailto:perl6-language-io@perl.org'>perl6-language-io@perl.org</a>
  Number: 311
  Version: 1
  Status: Developing</pre>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p><b>This</b> is what line disciplines are.</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<p>Line disciplines have been a much vaunted feature of 5.6, despite the
fact that nobody actually got around to implementing them. This time,
for sure!</p>
<p>First things first, what are they? Line disciplines are a way of
specifying how data gets into Perl. They're based on the concept of
streams, invented by Dennis Ritchie, and first appeared in Korn and Vo's
sfio library. Stevens writes that</p>
<pre>    I/O streams are generalized to represent both files and regions of
    memory, processing modules can be written and stacked on an I/O
    stream to change the operation of a stream, and better exception
    handling.</pre>
<p>How's this different from, for instance, generalizing source filters?
Well, that's how I first tried to implement them in Perl, but line
disciplines actually give you far, far more control over the file
handling; your processing modules may dictate how line endings are
parsed, whereas source filters have to go either before or after the
data is split up into lines. Line discipline processing modules may
alter the buffering behaviour of the stream, which you can't do in
standard IO. (That's a hint that we're going to have to provide our own
IO library to get these things working.)</p>
<p>OK, back to Perl. We'll want it to be possible to add these processing
modules onto filehandles from Perl, and (maybe) to create them in Perl.
We started doing this with <code>use open</code> and the extensions to the binmode
syntax. Benjamin Stuhl has done lots of good work on this (and this RFC
owes a huge amount to his suggestions) and he's come up with the
following API. From C, a processing module is registered like this:</p>
<pre>    PerlIO_register_discipline(char * name, int level, VTABLE functable,
    void * data);</pre>
<p>(We'll look at what <code>level</code> means when we come to implementation)</p>
<p>Once registered, a processing module can be attached to a file handle
through <code>binmode</code></p>
<pre>    binmode($FH, &quot;:+name&quot;):</pre>
<p>(Note: BKS originally suggested <code>+:name</code>, but I reversed this. Seemed a
good idea at the time.)</p>
<p>Here are a few examples:</p>
<pre>    open ($FH, &quot;&lt;&quot;, &quot;japanese.euc.gz&quot;);
    binmode($FH, &quot;:+decompress&quot;);
    binmode($FH, &quot;:+euc_to_utf8&quot;);
    $foo = &lt;$FH&gt;; # This now UTF8.</pre>
<p>Note that due to the concept of levels, this will still work:</p>
<pre>    open ($FH, &quot;&lt;&quot;, &quot;japanese.euc.gz&quot;);
    binmode($FH, &quot;:+euc_to_utf8&quot;);
    binmode($FH, &quot;:+decompress&quot;);</pre>
<p>I also propose that user-definable &quot;sets&quot; of processing modules can be
specified on the <code>open</code> line:</p>
<pre>    use open 'decompress_euc' =&gt; [ '+decompress', '+euc_to_utf8' ];
    open ($FH, &quot;&lt; :decompress_euc&quot;, &quot;japanese.euc.gz&quot;);</pre>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>Benjamin has identified 5 different types of transformation. Imagine
that the data goes through 5 &quot;rooms&quot; before it gets to Perl-space. Each
room can, in theory, have any numbers of processing modules in them, but
that's not actually workable at all in practise. Only levels 1 and 3 can
have several modules in them, and these modules will be implemented as a
stack.</p>
<p>Perl also needs to provide a default module for each &quot;room&quot;, and we'll
explain that as we look into the rooms.</p>
<p>(The example given is for input; simply walk through the rooms backwards
for output.)</p>
<ul>
<li><a name='Room 0: OS level'></a>Room 0: OS level</li>
<p>This level implements buffering; it's here that the difference between,
say, <code>sysread</code> and <code>read</code> becomes important. Modules in this layer
must be added on the <code>open</code> statement, since it controls very precisely
how Perl looks at the data even before we read anything from it.</p>
<p>The default behaviour is to emulate STDIO; in fact, the entirety of
STDIO apart from splitting the input into lines (<code>gets</code> and friends)
gets implemented here.</p>
<li><a name='Room 1: Byte Transformations'></a>Room 1: Byte Transformations</li>
<p>This is where things like decompression happen; you're performing
arbitrary transformations on the raw bytes.</p>
<p>Default behaviour is the operating system specific treament of carriage
returns and new lines, unless <code>:raw</code> is set by <code>binmode</code>.</p>
<li><a name='Room 2: Conversion to UTF8'></a>Room 2: Conversion to UTF8</li>
<p>What it says. Here the data has to be converted, if necessary, to UTF8.
I believe that this is Not Our Problem, as one of my other RFCs says. If
you want to convert from UTF8 to</p>
<p>The default behaviour is to convert the data from ISO8859-1 to UTF8 for
input and vice versa for output, unless <code>:utf8</code> is set to indicate that
we're already there, in which case no action is taken.</p>
<li><a name='Room 3: Transformations on the UTF8'></a>Room 3: Transformations on the UTF8</li>
<p>Whatever you want to do here. Default is no action.</p>
<li><a name='Room 4: Records'></a>Room 4: Records</li>
<p>It's at this point that the data gets split up into records or lines;
the equivalent of <code>$/</code> and <code>$\</code>.</p>
<p>The default behaviour is to split input on newlines and do nothing to
output.</p>
</ul>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p>W. Richard Stevens: Advanced Programming in the Unix Environment.</p>
<p>D. Ritchie: &quot;A Stream Input-Output System&quot;, AT&amp;T Bells Labs. Tech.
Journal, vol. 63, no. 8, pp.1897-1910</p>
<p>Korn and Vo: &quot;SFIO: Safe / Fast String / File IO&quot;, Proceedings of the
1991 Summer USENIX Conference, pp.235-255.</p>
<p>The sfio library.</p>
</div>
